package com.example.datachange.utils;

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

import java.io.FileWriter;
import java.io.IOException;

public class PdfUtils {

    public static void toTxt(String pdfPath, String txtPath) {
        //创建PdfDocument实例
        PdfDocument doc = new PdfDocument();
        //加载PDF文件  "C:\\Users\\tizzy\\Desktop\\测试.pdf"
        doc.loadFromFile(pdfPath);

        //创建StringBuilder实例
        StringBuilder sb = new StringBuilder();

        PdfPageBase page;
        //遍历PDF页面，获取每个页面的文本并添加到StringBuilder对象
        for (int i = 0; i < doc.getPages().getCount(); i++) {
            page = doc.getPages().get(i);
            sb.append(page.extractText(true));
        }
        FileWriter writer;
        try {
            //将StringBuilder对象中的文本写入到文本文件 "ExtractText.txt"
            writer = new FileWriter(txtPath);
            writer.write(sb.toString());
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            doc.close();
        }
    }

    public static void main(String[] args) {
        String pdfPath = "E:\\DataCaChe\\CM\\项目模块\\xxx\\xxxx-v1.2.pdf";
        String txtPath = "E:\\DataCaChe\\CM\\项目模块\\xxx\\xxxx-v1.2.txt";
        toTxt(pdfPath, txtPath);
    }
}
